package com.flyduck.web;

import com.flyduck.domain.SysMenu;
import com.flyduck.service.SysMenuService;
import com.flyduck.vo.AjaxResult;
import com.flyduck.vo.QueryVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Set;

/**
 * @program: flyduck-admin
 * @description: 菜单管理
 * @author: flyduck
 * @create: 2020-05-09 09:57
 **/
@Controller
@RequestMapping("menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 跳转到首页
     * @return
     */
    @RequiresPermissions("menu:list")
    @GetMapping
    public String index(){
        return "menu";
    }

    /**
     * 首页数据
     * @param queryVo
     * @return
     */
    @RequiresPermissions("menu:list")
    @GetMapping("list")
    @ResponseBody
    public Set<SysMenu> findList(QueryVo queryVo){
       return sysMenuService.findTree(queryVo);
    }

    /**
     * 前端菜单
     * @return
     */
    @GetMapping("build")
    @ResponseBody
    public Set<SysMenu> findList(){
        return sysMenuService.build();
    }

    /**
     * 保存菜单
     * @return
     */
    @RequiresPermissions("menu:save")
    @GetMapping("save")
    @ResponseBody
    public AjaxResult save(SysMenu sysMenu){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysMenuService.save(sysMenu);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("保存失败");
        }
        return ajaxResult;
    }

    /**
     * 更新菜单
     * @return
     */
    @RequiresPermissions("menu:update")
    @GetMapping("update")
    @ResponseBody
    public AjaxResult update(SysMenu sysMenu){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysMenuService.update(sysMenu);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("更新失败");
        }
        return ajaxResult;
    }

    /**
     * 根据菜单id删除当前及以下的所有菜单
     * @param id
     * @return
     */
    @RequiresPermissions("menu:delete")
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            sysMenuService.deleteById(id);
            ajaxResult.setSuccess(true);
            ajaxResult.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg("删除失败");
        }
        return ajaxResult;
    }

    /**
     * 根据菜单id查询菜单
     * @return
     */
    @GetMapping("/getMenuById")
    @ResponseBody
    public SysMenu getMenuById(Long id){
       return sysMenuService.getMenuById(id);
    }



}
